package com.chiang.myscrollview;

import com.chiang.myscrollview.ui.MyScrollView;
import com.chiang.myscrollview.ui.MyScrollView.OnPageChangedListener;

import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {

	private MyScrollView mScrollView;
	
	private RadioGroup mRadioGroup;
	
	//图片资源ID 数组
	private int[] mIds = new int[]{R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6};
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mScrollView = (MyScrollView)findViewById(R.id.myscrollview);
		mRadioGroup = (RadioGroup)findViewById(R.id.radioGroup);
		
		initView();
		
	}

	private void initView() {
		for(int i=0; i<mIds.length; i++){
			ImageView imageView = new ImageView(MainActivity.this);
			imageView.setBackgroundResource(mIds[i]);
			mScrollView.addView(imageView);
		}
		
		View view = View.inflate(MainActivity.this, R.layout.temp, null);
		mScrollView.addView(view, 2);
		
		for(int i=0; i<mScrollView.getChildCount(); i++){
			RadioButton radioButton = new RadioButton(MainActivity.this);
			radioButton.setId(i);
			if(i==0){
				radioButton.setChecked(true);
			}
			mRadioGroup.addView(radioButton);
		}
		
		mScrollView.setOnPageChangedListener(new OnPageChangedListener(){
			@Override
			public void onPageChanged(int currId) {
				((RadioButton)mRadioGroup.getChildAt(currId)).setChecked(true);
			}
			
		});
		
		mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				mScrollView.scrollToView(checkedId);
			}
		});
	}
}
